package com.jsync.model;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @author prabz
 *
 * @param <T>
 */
public class ResourceQueue extends LinkedList<Resource> {

	private Map<String, Long> filterMap;
	
	public ResourceQueue(){
		filterMap = new HashMap<String, Long>();
	}
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/* (non-Javadoc)
	 * @see java.util.LinkedList#add(java.lang.Object)
	 */
	@Override
	public boolean add(Resource e) {
		String path = e.getProfile().getSource()+File.separator+e.getName();
		File file = new File(path);
		if(Resource.Operation.MODIFY.equals(e.getChange())){
			Long modified = filterMap.get(path);
			if(modified!=null && modified.equals(file.lastModified())){
				return false;
			}
		}
		filterMap.put(path, file.lastModified());
		return super.add(e);
	}

	/* (non-Javadoc)
	 * @see java.util.LinkedList#remove()
	 */
	@Override
	public Resource remove() {	
		Resource e = super.remove();
		String path = e.getProfile().getSource()+File.separator+e.getName();
		filterMap.remove(path);
		return e;
	}
	
}
